#!/bin/sh
###############################################################################
##  Author    : xuezhoyi
##  Name      : edw_parallel_data_exporter.sh
##  Functions : 
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2017-05-22  xuezhouyi        1. CREATED THIS SHELL.
##  2.0      2018-01-24  mayanting 
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh

function USAGE(){
    echo -e "\nHow to use this shell script!"
    echo -e '\n${V_SHELL_HOME}/edw_parallel_data_exporter.sh 20170101 A mysql'
    echo -e '\n${V_SHELL_HOME}/edw_parallel_data_exporter.sh 20170101 A hive'
}

# Check variable
V_DATA_DATE=${1}
V_GROUP=${2}
V_MODE=${3}
if [[ -z ${V_DATA_DATE} || -z ${V_GROUP} || -z ${V_MODE} ]];then
    USAGE
    exit 1
fi

# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_SCRIPT_NAME}_${V_GROUP}_${V_MODE}_${V_TIME_STAMP}_$$.log"

# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output
echo -e "[Begin: $(date +%F) $(date +%T)]"

# Create data folder
if [[ ! -d "${V_SHELL_DATA}/${V_DATA_DATE}" ]];then
    mkdir -p "${V_SHELL_DATA}/${V_DATA_DATE}"
fi

# Export data
case ${V_MODE} in
    mysql)
        echo -e "\nReady to export group ${V_GROUP} ${V_MODE} data..."
        V_QUERY="SELECT CONCAT(TABLE_NAME,'|',SYNC_TYPE) FROM ETL.EDW_DATA_SYNC WHERE TABLE_GROUP = '${V_GROUP}' AND SYNC_TYPE = 'M2H';"
        V_TAB_LIST=$(${V_RUN_MYSQL} -q "${V_QUERY}")
        if [[ -n ${V_TAB_LIST} ]];then
            for TAB in ${V_TAB_LIST};do
                # Get the parameter
                V_TABLE_NAME=$(echo ${TAB} | awk -F'|' '{print $1}')
                
                echo -e "\n------export ${V_MODE} table ${V_TABLE_NAME}------"
                ${V_EXPT_DATA} -m mysql -t ${V_TABLE_NAME} -f "${V_SHELL_DATA}/${V_DATA_DATE}/${V_TABLE_NAME}.TXT"
                if [[ $? -eq 0 ]];then
                    touch "${V_SHELL_DATA}/${V_DATA_DATE}/${V_TABLE_NAME}.OK"
                else
                    exit 1
                fi
            done
        else
            echo -e "\nNo table to export."
        fi
    ;;
    hive)
        echo -e "\nReady to export group ${V_GROUP} ${V_MODE} data..."
        V_QUERY="SELECT CONCAT(TABLE_NAME,'|',SYNC_TYPE) FROM ETL.EDW_DATA_SYNC WHERE TABLE_GROUP = '${V_GROUP}' AND SYNC_TYPE = 'H2M';"
        V_TAB_LIST=$(${V_RUN_MYSQL} -q "${V_QUERY}")
        if [[ -n ${V_TAB_LIST} ]];then
            for TAB in ${V_TAB_LIST};do
                # Get the parameter
                V_TABLE_NAME=$(echo ${TAB} | awk -F'|' '{print $1}')
                
                echo -e "\n------export ${V_MODE} table ${V_TABLE_NAME}------"
                ${V_EXPT_DATA} -m hive -t ${V_TABLE_NAME} -f "${V_SHELL_DATA}/${V_DATA_DATE}/${V_TABLE_NAME}.TXT"
                if [[ $? -eq 0 ]];then
                    touch "${V_SHELL_DATA}/${V_DATA_DATE}/${V_TABLE_NAME}.OK"
                else
                    exit 1
                fi
            done
        else
            echo -e "\nNo table to export."
        fi
    ;;
esac

echo -e "\n[End: $(date +%F) $(date +%T)]"
exit 0
